package cn.goktech.util;

import lombok.Data;

import java.util.HashMap;
import java.util.Map;

@Data
public class ResultUtil {

        private boolean success;//请求状态
        private Integer code;//状态码
        private String msg;//返回信息
        private Map<String,Object> data;

        //构造器私有化-单例模式思想-用户不能随意创建对象
    private ResultUtil() {
    }

        public static ResultUtil ok(){
        ResultUtil rs = new ResultUtil();
        rs.setCode(200);
        rs.setSuccess(true);
        rs.setMsg("请求成功");
        rs.data = new HashMap<>();
        return rs;
    }

        public static ResultUtil error(){
        ResultUtil rs = new ResultUtil();
        rs.setCode(5000);
        rs.setSuccess(false);
        rs.setMsg("请求失败");
        rs.data = new HashMap<>();
        return rs;
    }

        //设置值返回this-支持链式调用
        public ResultUtil success(Boolean success){
        this.setSuccess(success);
        return this;
    }
        public ResultUtil message(String msg){
        this.setMsg(msg);
        return this;
    }
        public ResultUtil code(Integer code){
        this.setCode(code);
        return this;
    }
        public ResultUtil data(String key, Object value){
        this.data.put(key, value);
        return this;
    }
        public ResultUtil data(Map<String, Object> map) {
        this.setData(map);
        return this;
    }
    }

